import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ExtendHttpService} from '../../../common/extend-http.service';
import {Router} from '@angular/router';
import {TipService} from '../../services/tip.service';

@Component({
  selector: 'app-order-count-list',
  templateUrl: './order-count-list.component.html',
  styleUrls: ['./order-count-list.component.css']
})
export class OrderCountListComponent implements OnInit {
  isLoading = false;
  loading = true;
  isFlag = false;
  tipType = 'error';
  totalPage = 1;
  loadMoreFlag = true;
  list = [];
  params = {start: 1, startTime: '', endTime: '', is_free: '', is_entertain: '', is_credit: ''};
  isShowSpc: any = true;
  @Output() viewState = new EventEmitter();

  @Input() orderType: any;

  constructor(private extendHttp: ExtendHttpService,
              private router: Router,
              private tipService: TipService) {
  }

  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    };

    if (this.orderType) {
      this.isShowSpc = false;
      if (this.orderType === '1') {
        this.params.is_free = '1';
      }
      if (this.orderType === '2') {
        this.params.is_entertain = '1';
      }
      if (this.orderType === '3') {
        this.params.is_credit = '1';
      }
    }

    this.getData();
  }

  getData(searchFlag?: boolean) {
    // if (searchFlag) {
    //   if (!this.params.startTime.trim()) {
    //     this.tipService.change(true, '请选择开始时间');
    //     return;
    //   }
    //   if (!this.params.endTime.trim()) {
    //     this.tipService.change(true, '请选择结束时间');
    //     return;
    //   }
    // }

    this.extendHttp.post('/merchantApp/Statistics/getHistoricalOrderPage', this.params).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.list = data.page.list;
        this.params.start = data.page.pageNumber;
        this.totalPage = data.page.totalPage;
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  loadMore() {

    if (this.loadMoreFlag) {
      this.params.start = this.params.start + 1;
      this.loadMoreFlag = false;
      this.extendHttp.post('/merchantApp/Statistics/getHistoricalOrderPage', this.params).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.list = this.list.concat(data.page.list);
          this.params.start = data.page.pageNumber;
          this.totalPage = data.page.totalPage;
          this.loadMoreFlag = true;
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.params.start = this.params.start - 1;
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.loadMoreFlag = true;
        }
      }, error => {
        this.tipType = 'error';
        this.loadMoreFlag = true;
        this.params.start = this.params.start - 1;
        this.tipService.change(true, '系统繁忙，操作失败！');
      });
    }
  }

  clearData() {
    this.params.startTime = '';
    this.params.endTime = '';
  }

  getDetails(v) {
    this.viewState.emit(v.id);
  }

}
